home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / axcool / comdlg.cls < prev    next >
Encoding:
Visual Basic class definition  |  1998-10-26  |  35.2 KB  |  1,189 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "clsCommonDialog"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
  11. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  12. Option Explicit
  13.  
  14. Private m_cancelled As Boolean
  15. 'API function called by ChooseColor method
  16. Private Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As ChooseColor) As Long
  17.  
  18. 'API function called by ChooseFont method
  19. Private Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (pChoosefont As ChooseFontType) As Long
  20.  
  21. 'API function inside ShowHelp method
  22. Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" (ByVal hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData As Long) As Long
  23.  
  24. 'API function called by ShowOpen method
  25. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OpenFilename) As Long
  26.  
  27. 'API function called by ShowSave method
  28. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OpenFilename) As Long
  29.  
  30. 'API function called by ShowPrint method
  31. Private Declare Function PrintDlg Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PrintDlg) As Long
  32.  
  33.  
  34. 'API function to retrieve extended error information
  35. Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
  36.  
  37. 'API memory functions
  38. Private Declare Function GlobalAlloc Lib "KERNEL32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  39. Private Declare Function GlobalFree Lib "KERNEL32" (ByVal hMem As Long) As Long
  40. Private Declare Function GlobalLock Lib "KERNEL32" (ByVal hMem As Long) As Long
  41. Private Declare Function GlobalUnlock Lib "KERNEL32" (ByVal hMem As Long) As Long
  42.  
  43. Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" ( _
  44.          hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  45.  
  46. Private Const cdlCFANSIOnly = &H400    'Specifies that the dialog box allows only a selection of the fonts that use the Windows character set. If this flag is set, the user won't be able to select a font that contains only symbols.
  47. Private Const cdlCFApply = &H200   'Enables the Apply button on the dialog box.
  48. Private Const cdlCFBoth = &H3      'Causes the dialog box to list the available printer and screen fonts. The hDC property identifies the device context associated with the printer.
  49. Private Const cdlCFEffects = &H100     'Specifies that the dialog box enables strikethrough, underline, and color effects.
  50. Private Const cdlCFFixedPitchOnly = &H4000 'Specifies that the dialog box selects only fixed-pitch fonts.
  51. Private Const cdlCFForceFontExist = &H10000    'Specifies that an error message box is displayed if the user attempts to select a font or style that doesn't exist.
  52. Private Const cdlCFHelpButton = &H4    'Causes the dialog box to display a Help button.
  53. Private Const cdlCFLimitSize = &H2000  'Specifies that the dialog box selects only font sizes within the range specified by the Min and Max properties.
  54. Private Const cdlCFNoFaceSel = &H80000     'No font name selected.
  55. Private Const cdlCFNoSimulations = &H1000  'Specifies that the dialog box doesn't allow graphic device interface (GDI) font simulations.
  56. Private Const cdlCFNoSizeSel = &H200000    'No font size selected.
  57. Private Const cdlCFNoStyleSel = &H100000
  58. Private Const cdlCFNoVectorFonts = &H800   'Specifies that the dialog box doesn't allow vector-font selections.
  59. Private Const cdlCFPrinterFonts = &H2      'Causes the dialog box to list only the fonts supported by the printer, specified by the hDC property.
  60. Private Const cdlCFScalableOnly = &H20000      'Specifies that the dialog box allows only the selection of fonts that can be scaled.
  61. Private Const cdlCFScreenFonts = &H1       'Causes the dialog box to list only the screen fonts supported by the system.
  62. Private Const cdlCFTTOnly = &H40000    'Specifies that the dialog box allows only the selection of TrueType fonts.
  63. Private Const cdlCFWYSIWYG = &H8000    'Specifies that the dialog box allows only the selection of fonts that are available on both the printer and on screen. If this flag is set, the cdlCFBoth and cdlCFScalableOnly flags should also be set
  64.  
  65.  
  66.  
  67. 'constants for API memory functions
  68. Private Const GMEM_MOVEABLE = &H2
  69. Private Const GMEM_ZEROINIT = &H40
  70. Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
  71.  
  72.  
  73. 'data buffer for the ChooseColor function
  74. Private Type ChooseColor
  75.         lStructSize As Long
  76.         hwndOwner As Long
  77.         hInstance As Long
  78.         rgbResult As Long
  79.         lpCustColors As Long
  80.         flags As Long
  81.         lCustData As Long
  82.         lpfnHook As Long
  83.         lpTemplateName As String
  84. End Type
  85.  
  86. 'constants for LOGFONT
  87. Private Const LF_FACESIZE = 32
  88. Private Const LF_FULLFACESIZE = 64
  89. Private Const FW_BOLD = 700
  90.  
  91. 'data buffer for the ChooseFont function
  92. Private Type LOGFONT
  93.         lfHeight As Long
  94.         lfWidth As Long
  95.         lfEscapement As Long
  96.         lfOrientation As Long
  97.         lfWeight As Long
  98.         lfItalic As Byte
  99.         lfUnderline As Byte
  100.         lfStrikeOut As Byte
  101.         lfCharSet As Byte
  102.         lfOutPrecision As Byte
  103.         lfClipPrecision As Byte
  104.         lfQuality As Byte
  105.         lfPitchAndFamily As Byte
  106.         lfFaceName(LF_FACESIZE) As Byte
  107. End Type
  108.  
  109. 'data buffer for the ChooseFont function
  110. Private Type ChooseFontType
  111.         lStructSize As Long
  112.         hwndOwner As Long
  113.         hdc As Long
  114.         lpLogFont As Long
  115.         iPointSize As Long
  116.         flags As Long
  117.         rgbColors As Long
  118.         lCustData As Long
  119.         lpfnHook As Long
  120.         lpTemplateName As String
  121.         hInstance As Long
  122.         lpszStyle As String
  123.         nFontType As Integer
  124.         MISSING_ALIGNMENT As Integer
  125.         nSizeMin As Long
  126.         nSizeMax As Long
  127. End Type
  128.  
  129.  
  130. 'data buffer for the GetOpenFileName and GetSaveFileName functions
  131. Private Type OpenFilename
  132.         lStructSize As Long
  133.         hwndOwner As Long
  134.         hInstance As Long
  135.         lpstrFilter As String
  136.         lpstrCustomFilter As String
  137.         nMaxCustFilter As Long
  138.         iFilterIndex As Long
  139.         lpstrFile As String
  140.         nMaxFile As Long
  141.         lpstrFileTitle As String
  142.         nMaxFileTitle As Long
  143.         lpstrInitialDir As String
  144.         lpstrTitle As String
  145.         flags As Long
  146.         nFileOffset As Integer
  147.         nFileExtension As Integer
  148.         lpstrDefExt As String
  149.         lCustData As Long
  150.         lpfnHook As Long
  151.         lpTemplateName As String
  152. End Type
  153.  
  154.  
  155. 'data buffer for the PrintDlg function
  156. Private Type PrintDlg
  157.         lStructSize As Long
  158.         hwndOwner As Long
  159.         hDevMode As Long
  160.         hDevNames As Long
  161.         hdc As Long
  162.         flags As Long
  163.         nFromPage As Integer
  164.         nToPage As Integer
  165.         nMinPage As Integer
  166.         nMaxPage As Integer
  167.         nCopies As Integer
  168.         hInstance As Long
  169.         lCustData As Long
  170.         lpfnPrintHook As Long
  171.         lpfnSetupHook As Long
  172.         lpPrintTemplateName As String
  173.         lpSetupTemplateName As String
  174.         hPrintTemplate As Long
  175.         hSetupTemplate As Long
  176. End Type
  177.  
  178.  
  179. 'internal property buffers
  180.  
  181. Private iAction As Integer         'internal buffer for Action property
  182. Private bCancelError As Boolean    'internal buffer for CancelError property
  183. Private lColor As Long             'internal buffer for Color property
  184. Private lCopies As Long            'internal buffer for lCopies property
  185. Private sDefaultExt As String      'internal buffer for sDefaultExt property
  186. Private sDialogTitle As String     'internal buffer for DialogTitle property
  187. Private sFileName As String        'internal buffer for FileName property
  188. Private sFileTitle As String       'internal buffer for FileTitle property
  189. Private sFilter As String          'internal buffer for Filter property
  190. Private iFilterIndex As Integer    'internal buffer for FilterIndex property
  191. Private lFlags As Long             'internal buffer for Flags property
  192. Private bFontBold As Boolean       'internal buffer for FontBold property
  193. Private bFontItalic As Boolean     'internal buffer for FontItalic property
  194. Private sFontName As String        'internal buffer for FontName property
  195. Private lFontSize As Long          'internal buffer for FontSize property
  196. Private bFontStrikethru As Boolean 'internal buffer for FontStrikethru property
  197. Private bFontUnderline As Boolean  'internal buffer for FontUnderline property
  198. Private lFromPage As Long          'internal buffer for FromPage property
  199. Private lhdc As Long               'internal buffer for hdc property
  200. Private lHelpCommand As Long       'internal buffer for HelpCommand property
  201. Private sHelpContext As String     'internal buffer for HelpContext property
  202. Private sHelpFile As String        'internal buffer for HelpFile property
  203. Private sHelpKey As String         'internal buffer for HelpKey property
  204. Private sInitDir As String         'internal buffer for InitDir property
  205. Private lMax As Long               'internal buffer for Max property
  206. Private lMaxFileSize As Long       'internal buffer for MaxFileSize property
  207. Private lMin As Long               'internal buffer for Min property
  208. Private objObject As Object        'internal buffer for Object property
  209. Private iPrinterDefault As Integer 'internal buffer for PrinterDefault property
  210. Private lToPage As Long            'internal buffer for ToPage property
  211.  
  212. Private lApiReturn As Long          'internal buffer for APIReturn property
  213. Private lExtendedError As Long      'internal buffer for ExtendedError property
  214.  
  215.  
  216.  
  217. 'constants for color dialog
  218.  
  219. Private Const CDERR_DIALOGFAILURE = &HFFFF
  220. Private Const CDERR_FINDRESFAILURE = &H6
  221. Private Const CDERR_GENERALCODES = &H0
  222. Private Const CDERR_INITIALIZATION = &H2
  223. Private Const CDERR_LOADRESFAILURE = &H7
  224. Private Const CDERR_LOADSTRFAILURE = &H5
  225. Private Const CDERR_LOCKRESFAILURE = &H8
  226. Private Const CDERR_MEMALLOCFAILURE = &H9
  227. Private Const CDERR_MEMLOCKFAILURE = &HA
  228. Private Const CDERR_NOHINSTANCE = &H4
  229. Private Const CDERR_NOHOOK = &HB
  230. Private Const CDERR_NOTEMPLATE = &H3
  231. Private Const CDERR_REGISTERMSGFAIL = &HC
  232. Private Const CDERR_STRUCTSIZE = &H1
  233.  
  234.  
  235. 'constants for file dialog
  236.  
  237. Private Const FNERR_BUFFERTOOSMALL = &H3003
  238. Private Const FNERR_FILENAMECODES = &H3000
  239. Private Const FNERR_INVALIDFILENAME = &H3002
  240. Private Const FNERR_SUBCLASSFAILURE = &H3001
  241.  
  242. Public Property Get Filter() As String
  243.     'return object's Filter property
  244.     Filter = sFilter
  245.  
  246. End Property
  247.  
  248. Public Sub ShowColor()
  249.     'display the color dialog box
  250.     
  251.     Dim tChooseColor As ChooseColor
  252.     Dim alCustomColors(15) As Long
  253.     Dim lCustomColorSize As Long
  254.     Dim lCustomColorAddress As Long
  255.     Dim lMemHandle As Long
  256.     
  257.     Dim n As Integer
  258.         
  259.     On Error GoTo ShowColorError
  260.     
  261.     
  262.     '***    init property buffers
  263.     
  264.     iAction = 3  'Action property - ShowColor
  265.     lApiReturn = 0  'APIReturn property
  266.     lExtendedError = 0  'ExtendedError property
  267.     
  268.     
  269.     '***    prepare tChooseColor data
  270.     
  271.     'lStructSize As Long
  272.     tChooseColor.lStructSize = Len(tChooseColor)
  273.     
  274.     'hwndOwner As Long
  275.     tChooseColor.hwndOwner = lhdc
  276.  
  277.     'hInstance As Long
  278.     
  279.     'rgbResult As Long
  280.     tChooseColor.rgbResult = lColor
  281.     
  282.     'lpCustColors As Long
  283.     ' Fill custom colors array with all white
  284.     For n = 0 To UBound(alCustomColors)
  285.         alCustomColors(n) = &HFFFFFF
  286.     Next
  287.     ' Get size of memory needed for custom colors
  288.     lCustomColorSize = Len(alCustomColors(0)) * 16
  289.     ' Get a global memory block to hold a copy of the custom colors
  290.     lMemHandle = GlobalAlloc(GHND, lCustomColorSize)
  291.     
  292.     If lMemHandle = 0 Then
  293.         Exit Sub
  294.     End If
  295.     ' Lock the custom color's global memory block
  296.     lCustomColorAddress = GlobalLock(lMemHandle)
  297.     If lCustomColorAddress = 0 Then
  298.         Exit Sub
  299.     End If
  300.     ' Copy custom colors to the global memory block
  301.     Call CopyMemory(ByVal lCustomColorAddress, alCustomColors(0), lCustomColorSize)
  302.  
  303.     tChooseColor.lpCustColors = lCustomColorAddress
  304.     
  305.     'flags As Long
  306.     tChooseColor.flags = lFlags
  307.         
  308.     'lCustData As Long
  309.     'lpfnHook As Long
  310.     'lpTemplateName As String
  311.     
  312.     m_cancelled = False
  313.     '***    call the ChooseColor API function
  314.     lApiReturn = ChooseColor(tChooseColor)
  315.     
  316.     
  317.     '***    handle return from ChooseColor API function
  318.     Select Case lApiReturn
  319.         
  320.         Case 0  'user canceled
  321.         If bCancelError = True Then
  322.             'generate an error
  323.             
  324.             m_cancelled = True
  325.             Exit Sub
  326.         End If
  327.         
  328.         Case 1  'user selected a color
  329.             'update property buffer
  330.             lColor = tChooseColor.rgbResult
  331.         
  332.         Case Else   'an error occured
  333.             'call CommDlgExtendedError
  334.             lExtendedError = CommDlgExtendedError
  335.         
  336.     End Select
  337.  
  338. Exit Sub
  339.  
  340. ShowColorError:
  341.     Exit Sub
  342. End Sub
  343.  
  344. Public Sub ShowFont()
  345.     'display the font dialog box
  346.  
  347.     Dim tLogFont As LOGFONT
  348.     Dim tChooseFont As ChooseFontType
  349.  
  350.     Dim lLogFontSize As Long
  351.     Dim lLogFontAddress As Long
  352.     Dim lMemHandle As Long
  353.  
  354.     Dim lReturn As Long
  355.     Dim sFont As String
  356.     Dim lBytePoint As Long
  357.     On Error GoTo ShowFontError
  358.  
  359.     '***    init property buffers
  360.  
  361.     iAction = 4  'Action property - ShowFont
  362.     lApiReturn = 0  'APIReturn property
  363.     lExtendedError = 0  'ExtendedError property
  364.  
  365.  
  366.     '***    prepare tChooseFont data
  367.  
  368.     'tLogFont.lfHeight As Long
  369.     'tLogFont.lfWidth As Long
  370.     'tLogFont.lfEscapement As Long
  371.     'tLogFont.lfOrientation As Long
  372.  
  373.     'tLogFont.lfWeight As Long - init from FontBold property
  374.     If bFontBold = True Then
  375.         tLogFont.lfWeight = FW_BOLD
  376.     End If
  377.  
  378.     'tLogFont.lfItalic As Byte - init from FontItalic property
  379.     If bFontItalic = True Then
  380.         tLogFont.lfItalic = 1
  381.     End If
  382.  
  383.     'tLogFont.lfUnderline As Byte - init from FontUnderline property
  384.     If bFontUnderline = True Then
  385.         tLogFont.lfUnderline = 1
  386.     End If
  387.  
  388.     'tLogFont.lfStrikeOut As Byte - init from FontStrikethru property
  389.     If bFontStrikethru = True Then
  390.         tLogFont.lfStrikeOut = 1
  391.     End If
  392.  
  393.  
  394.     'tLogFont.lfCharSet As Byte
  395.     'tLogFont.lfOutPrecision As Byte
  396.     'tLogFont.lfClipPrecision As Byte
  397.     'tLogFont.lfQuality As Byte
  398.     'tLogFont.lfPitchAndFamily As Byte
  399.     'tLogFont.lfFaceName(LF_FACESIZE) As Byte
  400.  
  401.     'tChooseFont.lStructSize As Long
  402.     tChooseFont.lStructSize = Len(tChooseFont)
  403.  
  404.     'tChooseFont.hwndOwner As Long
  405.     'tChooseFont.hdc As Long
  406.  
  407.     'tChooseFont.lpLogFont As Long
  408.     lLogFontSize = Len(tLogFont)
  409.  
  410.     ' Get a global memory block to hold a copy of tLogFont - exit on failure
  411.     lMemHandle = GlobalAlloc(GHND, lLogFontSize)
  412.     If lMemHandle = 0 Then
  413.         Exit Sub
  414.     End If
  415.  
  416.     ' Lock tLogFont's global memory block - exit on failure
  417.     lLogFontAddress = GlobalLock(lMemHandle)
  418.     If lLogFontAddress = 0 Then
  419.         Exit Sub
  420.     End If
  421.  
  422.  
  423.     ' Copy tLogFont to the global memory block
  424.     Call CopyMemory(ByVal lLogFontAddress, tLogFont, lLogFontSize)
  425.  
  426.     tChooseFont.lpLogFont = lLogFontAddress
  427.  
  428.     'tChooseFont.iPointSize As Long - init from FontSize property
  429.     tChooseFont.iPointSize = lFontSize * 10
  430.  
  431.     tChooseFont.rgbColors = lColor
  432.  
  433.  
  434.     'tChooseFont.flags As Long - init from Flags property
  435.     tChooseFont.flags = cdlCFScreenFonts Or cdlCFEffects 'lFlags
  436.  
  437.     'tChooseFont.rgbColors As Long
  438.     'tChooseFont.lCustData As Long
  439.     'tChooseFont.lpfnHook As Long
  440.     'tChooseFont.lpTemplateName As String
  441.     'tChooseFont.hInstance As Long
  442.  
  443.     'tChooseFont.lpszStyle As String
  444.     'sFont = Chr$(0) & Space$(20) & Chr$(0)
  445.  
  446.  
  447.     'tChooseFont.nFontType As Integer
  448.     'tChooseFont.MISSING_ALIGNMENT As Integer
  449.     'tChooseFont.nSizeMin As Long
  450.     'tChooseFont.nSizeMax As Long
  451.  
  452.  
  453.     '***    call the CHOOSEFONT API function
  454.     lApiReturn = CHOOSEFONT(tChooseFont)    'store to APIReturn property
  455.  
  456.  
  457.     '***    handle return from CHOOSEFONT API function
  458.     Select Case lApiReturn
  459.  
  460.         Case 0  'user canceled
  461.         If bCancelError = True Then
  462.             'generate an error
  463.             Err.Raise (2001)
  464.             Exit Sub
  465.         End If
  466.  
  467.         Case 1  'user selected a font
  468.             ' Copy global memory block to tLogFont
  469.             Call CopyMemory(tLogFont, ByVal lLogFontAddress, lLogFontSize)
  470.  
  471.             lColor = tChooseFont.rgbColors
  472.  
  473.             'tLogFont.lfWeight As Long  - store to FontBold property
  474.             If tLogFont.lfWeight >= FW_BOLD Then
  475.                 bFontBold = True
  476.             Else
  477.                 bFontBold = False
  478.             End If
  479.  
  480.             'tLogFont.lfItalic As Byte - store to FontItalic property
  481.             If tLogFont.lfItalic = 1 Then
  482.                 bFontItalic = True
  483.             Else
  484.                 bFontItalic = False
  485.             End If
  486.  
  487.             'tLogFont.lfUnderline As Byte - store to FontUnderline property
  488.             If tLogFont.lfUnderline = 1 Then
  489.                 bFontUnderline = True
  490.             Else
  491.                 bFontUnderline = False
  492.             End If
  493.  
  494.             'tLogFont.lfStrikeOut As Byte - store to FontStrikethru property
  495.             If tLogFont.lfStrikeOut = 1 Then
  496.                 bFontStrikethru = True
  497.             Else
  498.                 bFontStrikethru = False
  499.             End If
  500.  
  501.             'tLogFont.lfFaceName(LF_FACESIZE) As Byte - store to FontName property
  502.             FontName = sByteArrayToString(tLogFont.lfFaceName())
  503.  
  504.             'tChooseFont.iPointSize As Long - store to FontSize property
  505.             lFontSize = CLng(tChooseFont.iPointSize / 10)
  506.  
  507.         Case Else   'an error occured
  508.             'call CommDlgExtendedError
  509.             lExtendedError = CommDlgExtendedError   'store to ExtendedError property
  510.  
  511.     End Select
  512. Exit Sub
  513.  
  514. ShowFontError:
  515.     Exit Sub
  516. End Sub
  517.  
  518. Public Sub ShowHelp()
  519.     'run winhelp.exe with the specified help file
  520.     Dim sHelpFileBuff As String
  521.     Dim lData As Long
  522.  
  523.     On Error GoTo ShowHelpError
  524.  
  525.     '***    init Private properties
  526.     iAction = 6  'Action property - ShowHelp
  527.     lApiReturn = 0  'APIReturn property
  528.     lExtendedError = 0  'ExtendedError property
  529.  
  530.     '***    prepare the buffers and parameters for the API function
  531.     'sHelpFile is a null terminated string
  532.     sHelpFileBuff = sHelpFile & Chr$(0)
  533.  
  534.     'sData is dependent on lHelpCommand
  535.     Select Case lHelpCommand
  536.         Case 0
  537.             lData = 0
  538.         Case Else
  539.             lData = 0
  540.     End Select
  541.  
  542.     '***    call the API function
  543.     lApiReturn = WinHelp(lhdc, sHelpFile, lHelpCommand, lData)    ' - Store to APIReturn property
  544.  
  545.     Select Case lApiReturn
  546.  
  547.         Case 0  '
  548.             'call CommDlgExtendedError
  549.             lExtendedError = CommDlgExtendedError   ' - store to ExtendedError property
  550.  
  551.         Case Else   '
  552.             'call CommDlgExtendedError
  553.             lExtendedError = CommDlgExtendedError
  554.  
  555.     End Select
  556.  
  557. Exit Sub
  558.  
  559. ShowHelpError:
  560.     Exit Sub
  561.  
  562. End Sub
  563.  
  564.  
  565. Public Sub ShowOpen()
  566.     
  567.     'display the file open dialog box
  568.     ShowFileDialog (1)  'Action property - ShowOpen
  569.     
  570. End Sub
  571.  
  572. Public Sub ShowPrinter()
  573.     'display the print dialog
  574.     Dim tPrintDlg As PrintDlg
  575.  
  576.     On Error GoTo ShowPrinterError
  577.  
  578.     '***    init public properties
  579.     iAction = 5  'Action property - ShowPrint
  580.     lApiReturn = 0  'APIReturn property
  581.     lExtendedError = 0  'ExtendedError property
  582.  
  583.     '***    prepare tPrintDlg data
  584.  
  585.     'lStructSize As Long
  586.     tPrintDlg.lStructSize = Len(tPrintDlg)
  587.  
  588.     'hwndOwner As Long
  589.  
  590.     'hDevMode As Long
  591.  
  592.     'hDevNames As Long
  593.  
  594.     'hdc As Long - init from hDC property
  595.     tPrintDlg.hdc = lhdc
  596.  
  597.     'flags As Long - init from Flags property
  598.     tPrintDlg.flags = lFlags
  599.  
  600.     'nFromPage As Integer - init from FromPage property
  601.     tPrintDlg.nFromPage = lFromPage
  602.  
  603.     'nToPage As Integer - init from ToPage property
  604.     tPrintDlg.nToPage = lToPage
  605.  
  606.     'nMinPage As Integer - init from Min property
  607.     tPrintDlg.nMinPage = lMin
  608.  
  609.     'nMaxPage As Integer - init from Max property
  610.     tPrintDlg.nMaxPage = lMax
  611.  
  612.     'nCopies As Integer - init from Copies property
  613.     tPrintDlg.nCopies = lCopies
  614.  
  615.     'hInstance As Long
  616.  
  617.     'lCustData As Long
  618.  
  619.  
  620.     '***    Call the PrintDlg API function
  621.     lApiReturn = PrintDlg(tPrintDlg)
  622.  
  623.     '***    handle return from PrintDlg API function
  624.     Select Case lApiReturn
  625.  
  626.         Case 0  'user canceled
  627.             If bCancelError = True Then
  628.                 'generate an error
  629.                 Err.Raise (2001)
  630.                 Exit Sub
  631.             End If
  632.  
  633.         Case 1  'user selected OK
  634.             'nFromPage As Integer - store to FromPage property
  635.             lFromPage = tPrintDlg.nFromPage
  636.  
  637.             'nToPage As Integer - store to ToPage property
  638.             lToPage = tPrintDlg.nToPage
  639.  
  640.             'nMinPage As Integer - store to Min property
  641.             lMin = tPrintDlg.nMinPage
  642.  
  643.             'nMaxPage As Integer - store to Max property
  644.             lMax = tPrintDlg.nMaxPage
  645.  
  646.             'nCopies As Integer - store to Copies property
  647.             lCopies = tPrintDlg.nCopies
  648.  
  649.         Case Else   'an error occured
  650.             'call CommDlgExtendedError
  651.             lExtendedError = CommDlgExtendedError   'store to ExtendedError property
  652.  
  653.     End Select
  654.  
  655. Exit Sub
  656.  
  657. ShowPrinterError:
  658.  
  659.     Exit Sub
  660.  
  661. End Sub
  662.  
  663.  
  664. Public Sub ShowSave()
  665.  
  666.     'display the file save dialog box
  667.     ShowFileDialog (2)  'Action property - ShowSave
  668.  
  669. End Sub
  670.  
  671.  
  672. Public Property Get FileName() As String
  673.     'return object's FileName property
  674.     FileName = sFileName
  675. End Property
  676.  
  677. Public Property Let FileName(vNewValue As String)
  678.     'assign object's FileName property
  679.     sFileName = vNewValue
  680. End Property
  681.  
  682.  
  683. Public Property Let Filter(vNewValue As String)
  684.     'assign object's Filter property
  685.     sFilter = vNewValue
  686. End Property
  687.  
  688.  
  689. Private Function sLeftOfNull(ByVal sIn As String)
  690.     'returns the part of sIn preceding Chr$(0)
  691.     Dim lNullPos As Long
  692.     
  693.     'init output
  694.     sLeftOfNull = sIn
  695.     
  696.     'get position of first Chr$(0) in sIn
  697.     lNullPos = InStr(sIn, Chr$(0))
  698.     
  699.     'return part of sIn to left of first Chr$(0) if found
  700.     If lNullPos > 0 Then
  701.         sLeftOfNull = Mid$(sIn, 1, lNullPos - 1)
  702.     End If
  703.     
  704. End Function
  705.  
  706.  
  707. Public Property Get Action() As Integer
  708.     'Return object's Action property
  709.     Action = iAction
  710. End Property
  711.  
  712. Private Function sAPIFilter(sIn)
  713.     'prepares sIn for use as a filter string in API common dialog functions
  714.     Dim lChrNdx As Long
  715.     Dim sOneChr As String
  716.     Dim sOutStr As String
  717.     
  718.     'convert any | characters to nulls
  719.     For lChrNdx = 1 To Len(sIn)
  720.         sOneChr = Mid$(sIn, lChrNdx, 1)
  721.         If sOneChr = "|" Then
  722.             sOutStr = sOutStr & Chr$(0)
  723.         Else
  724.             sOutStr = sOutStr & sOneChr
  725.         End If
  726.     Next
  727.     
  728.     'add a null to the end
  729.     sOutStr = sOutStr & Chr$(0)
  730.     
  731.     'return sOutStr
  732.     sAPIFilter = sOutStr
  733.     
  734. End Function
  735.  
  736. Public Property Get FilterIndex() As Integer
  737.     'return object's FilterIndex property
  738.     FilterIndex = iFilterIndex
  739. End Property
  740.  
  741. Public Property Let FilterIndex(vNewValue As Integer)
  742.     iFilterIndex = vNewValue
  743. End Property
  744.  
  745. Public Property Get CancelError() As Boolean
  746.     'Return object's CancelError property
  747.     CancelError = bCancelError
  748. End Property
  749.  
  750. Public Property Let CancelError(vNewValue As Boolean)
  751.     'Assign object's CancelError property
  752.     bCancelError = vNewValue
  753. End Property
  754.  
  755. Public Property Get Color() As Long
  756.     'return object's Color property
  757.     Color = lColor
  758. End Property
  759.  
  760. Public Property Let Color(vNewValue As Long)
  761.     'assign object's Color property
  762.     lColor = vNewValue
  763. End Property
  764.  
  765. Public Property Get Copies() As Long
  766.     'return object's Copies property
  767.     Copies = lCopies
  768. End Property
  769.  
  770. Public Property Let Copies(vNewValue As Long)
  771.     'assign object's Copies property
  772.     lCopies = vNewValue
  773. End Property
  774.  
  775. Public Property Get DefaultExt() As String
  776.     'return object's DefaultExt property
  777.     DefaultExt = sDefaultExt
  778. End Property
  779.  
  780. Public Property Let DefaultExt(vNewValue As String)
  781.     'assign object's DefaultExt property
  782.     sDefaultExt = vNewValue
  783. End Property
  784.  
  785. Public Property Get DialogTitle() As String
  786.     'return object's FileName property
  787.     DialogTitle = sDialogTitle
  788. End Property
  789.  
  790. Public Property Let DialogTitle(vNewValue As String)
  791.     'assign object's DialogTitle property
  792.     sDialogTitle = vNewValue
  793. End Property
  794.  
  795. Public Property Get flags() As Long
  796.     'return object's Flags property
  797.     flags = lFlags
  798. End Property
  799.  
  800. Public Property Let flags(vNewValue As Long)
  801.     'assign object's Flags property
  802.     lFlags = vNewValue
  803. End Property
  804.  
  805. Public Property Get FontBold() As Boolean
  806.     'return object's FontBold property
  807.     FontBold = bFontBold
  808. End Property
  809.  
  810. Public Property Let FontBold(vNewValue As Boolean)
  811.     'Assign object's FontBold property
  812.     bFontBold = vNewValue
  813. End Property
  814.  
  815. Public Property Get FontItalic() As Boolean
  816.     'Return object's FontItalic property
  817.     FontItalic = bFontItalic
  818. End Property
  819.  
  820. Public Property Let FontItalic(vNewValue As Boolean)
  821.     'Assign object's FontItalic property
  822.     bFontItalic = vNewValue
  823. End Property
  824.  
  825. Public Property Get FontName() As String
  826.     'Return object's Fontname property
  827.     FontName = sFontName
  828. End Property
  829.  
  830. Public Property Let FontName(vNewValue As String)
  831.     'Assign object's FontName property
  832.     sFontName = vNewValue
  833. End Property
  834.  
  835. Public Property Get FontSize() As Long
  836.     'Return object's FontSize property
  837.     FontSize = lFontSize
  838. End Property
  839.  
  840. Public Property Let FontSize(vNewValue As Long)
  841.     'Assign object's FontSize property
  842.     lFontSize = vNewValue
  843. End Property
  844.  
  845. Public Property Get FontStrikethru() As Boolean
  846.     'Return object's FontStrikethru property
  847.     FontStrikethru = bFontStrikethru
  848. End Property
  849.  
  850. Public Property Let FontStrikethru(vNewValue As Boolean)
  851.     'Assign object's - property
  852.     bFontStrikethru = vNewValue
  853. End Property
  854.  
  855. Public Property Get FontUnderline() As Boolean
  856.     'Return object's FontUnderline property
  857.     FontUnderline = bFontUnderline
  858. End Property
  859.  
  860. Public Property Let FontUnderline(vNewValue As Boolean)
  861.     'Assign object's FontUnderline property
  862.     bFontUnderline = vNewValue
  863. End Property
  864.  
  865. Public Property Get FromPage() As Long
  866.     'Return object's FromPAge property
  867.     FromPage = lFromPage
  868. End Property
  869.  
  870. Public Property Let FromPage(vNewValue As Long)
  871.     'Assign object's FromPage property
  872.     lFromPage = vNewValue
  873. End Property
  874.  
  875. Public Property Get hdc() As Long
  876.     'Return object's hDC property
  877.     hdc = lhdc
  878. End Property
  879.  
  880. Public Property Let hdc(vNewValue As Long)
  881.     'Assign object's hDC property
  882.     lhdc = vNewValue
  883. End Property
  884.  
  885. Public Property Get HelpCommand() As Long
  886.     'Return object's HelpCommand property
  887.     HelpCommand = lHelpCommand
  888. End Property
  889.  
  890. Public Property Let HelpCommand(vNewValue As Long)
  891.     'Assign object's HelpCommand property
  892.     lHelpCommand = vNewValue
  893. End Property
  894.  
  895. Public Property Get HelpContext() As String
  896.     'Return object's HelpContext property
  897.     HelpContext = sHelpContext
  898. End Property
  899.  
  900. Public Property Let HelpContext(vNewValue As String)
  901.     'Assign object's HelpContext property
  902.     sHelpContext = vNewValue
  903. End Property
  904.  
  905. Public Property Get HelpFile() As String
  906.     'Return object's HelpFile property
  907.     HelpFile = sHelpFile
  908. End Property
  909.  
  910. Public Property Let HelpFile(vNewValue As String)
  911.     'Assign object's HelpFile property
  912.     sHelpFile = vNewValue
  913. End Property
  914.  
  915. Public Property Get HelpKey() As String
  916.     'Return object's HelpKey property
  917.     HelpKey = sHelpKey
  918. End Property
  919.  
  920. Public Property Let HelpKey(vNewValue As String)
  921.     'Assign object's HelpKey property
  922.     sHelpKey = vNewValue
  923. End Property
  924.  
  925. Public Property Get InitDir() As String
  926.     'Return object's InitDir property
  927.     InitDir = sInitDir
  928. End Property
  929.  
  930. Public Property Let InitDir(vNewValue As String)
  931.     'Assign object's InitDir property
  932.     sInitDir = vNewValue
  933. End Property
  934.  
  935. Public Property Get Max() As Long
  936.     'Return object's Max property
  937.     Max = lMax
  938. End Property
  939.  
  940. Public Property Let Max(vNewValue As Long)
  941.     'Assign object's - property
  942.     lMax = vNewValue
  943. End Property
  944.  
  945. Public Property Get MaxFileSize() As Long
  946.     'Return object's MaxFileSize property
  947.     MaxFileSize = lMaxFileSize
  948. End Property
  949.  
  950. Public Property Let MaxFileSize(vNewValue As Long)
  951.     'Assign object's MaxFileSize property
  952.     lMaxFileSize = vNewValue
  953. End Property
  954.  
  955. Public Property Get Min() As Long
  956.     'Return object's Min property
  957.     Min = lMin
  958. End Property
  959.  
  960. Public Property Let Min(vNewValue As Long)
  961.     'Assign object's Min property
  962.     lMin = vNewValue
  963. End Property
  964.  
  965. Public Property Get Object() As Object
  966.     'Return object's Object property
  967.     Object = objObject
  968. End Property
  969.  
  970. Public Property Let Object(vNewValue As Object)
  971.     'Assign object's Object property
  972.     objObject = vNewValue
  973. End Property
  974.  
  975. 'Public Property Get PrinterDefault() As Integer
  976. '    'Return object's PrinterDefault property
  977. '    PrinterDefault = iPrinterDefault
  978. 'End Property
  979. '
  980. 'Public Property Let PrinterDefault(vNewValue As Integer)
  981. '    'Assign object's PrinterDefault property
  982. '    iPrinterDefault = vNewValue
  983. 'End Property
  984. '
  985. 'Public Property Get ToPage() As Long
  986. '    'Return object's ToPage property
  987. '    ToPage = lToPage
  988. 'End Property
  989. '
  990. 'Public Property Let ToPage(vNewValue As Long)
  991. '    'Assign object's ToPage property
  992. '    lToPage = vNewValue
  993. 'End Property
  994. '
  995. Public Property Get FileTitle() As String
  996.     'return object's FileTitle property
  997.     FileTitle = sFileTitle
  998. End Property
  999.  
  1000. Public Property Let FileTitle(vNewValue As String)
  1001.     'assign object's FileTitle property
  1002.     sFileTitle = vNewValue
  1003. End Property
  1004.  
  1005. Public Property Get APIReturn() As Long
  1006.     'return object's APIReturn property
  1007.     APIReturn = lApiReturn
  1008. End Property
  1009.  
  1010. Public Property Get ExtendedError() As Long
  1011.     'return object's ExtendedError property
  1012.     ExtendedError = lExtendedError
  1013. End Property
  1014.  
  1015.  
  1016. Private Function sByteArrayToString(abBytes() As Byte) As String
  1017.     'return a string from a byte array
  1018.     Dim lBytePoint As Long
  1019.     Dim lByteVal As Long
  1020.     Dim sOut As String
  1021.     
  1022.     'init array pointer
  1023.     lBytePoint = LBound(abBytes)
  1024.     
  1025.     'fill sOut with characters in array
  1026.     While lBytePoint <= UBound(abBytes)
  1027.         
  1028.         lByteVal = abBytes(lBytePoint)
  1029.         
  1030.         'return sOut and stop if Chr$(0) is encountered
  1031.         If lByteVal = 0 Then
  1032.             sByteArrayToString = sOut
  1033.             Exit Function
  1034.         Else
  1035.             sOut = sOut & Chr$(lByteVal)
  1036.         End If
  1037.         
  1038.         lBytePoint = lBytePoint + 1
  1039.     
  1040.     Wend
  1041.     
  1042.     'return sOut if Chr$(0) wasn't encountered
  1043.     sByteArrayToString = sOut
  1044.     
  1045. End Function
  1046. Private Sub ShowFileDialog(ByVal iAction As Integer)
  1047.     
  1048.     'display the file dialog for ShowOpen or ShowSave
  1049.     
  1050.     Dim tOpenFile As OpenFilename
  1051.     Dim lMaxSize As Long
  1052.     Dim sFileNameBuff As String
  1053.     Dim sFileTitleBuff As String
  1054.     
  1055.     On Error GoTo ShowFileDialogError
  1056.     
  1057.     
  1058.     '***    init property buffers
  1059.     
  1060.     iAction = iAction  'Action property
  1061.     lApiReturn = 0  'APIReturn property
  1062.     lExtendedError = 0  'ExtendedError property
  1063.         
  1064.     
  1065.     '***    prepare tOpenFile data
  1066.     
  1067.     'tOpenFile.lStructSize As Long
  1068.     tOpenFile.lStructSize = Len(tOpenFile)
  1069.     
  1070.     'tOpenFile.hWndOwner As Long - init from hdc property
  1071.     tOpenFile.hwndOwner = lhdc
  1072.     
  1073.     'tOpenFile.lpstrFilter As String - init from Filter property
  1074.     tOpenFile.lpstrFilter = sAPIFilter(sFilter)
  1075.         
  1076.     'tOpenFile.iFilterIndex As Long - init from FilterIndex property
  1077.     tOpenFile.iFilterIndex = iFilterIndex
  1078.     
  1079.     'tOpenFile.lpstrFile As String
  1080.         'determine size of buffer from MaxFileSize property
  1081.         If lMaxFileSize > 0 Then
  1082.             lMaxSize = lMaxFileSize
  1083.         Else
  1084.             lMaxSize = 255
  1085.         End If
  1086.     
  1087.     'tOpenFile.lpstrFile As Long - init from FileName property
  1088.         'prepare sFileNameBuff
  1089.         sFileNameBuff = sFileName
  1090.         'pad with spaces
  1091.         While Len(sFileNameBuff) < lMaxSize - 1
  1092.             sFileNameBuff = sFileNameBuff & " "
  1093.         Wend
  1094.         'trim to length of lMaxFileSize - 1
  1095.         If lMaxFileSize = 0 Then
  1096.             sFileNameBuff = Mid$(sFileNameBuff, 1, lMaxSize - 1)
  1097.         Else
  1098.             sFileNameBuff = Mid$(sFileNameBuff, 1, lMaxFileSize - 1)
  1099.         End If
  1100.         'null terminate
  1101.         sFileNameBuff = sFileNameBuff & Chr$(0)
  1102.     tOpenFile.lpstrFile = sFileNameBuff
  1103.     
  1104.     'nMaxFile As Long - init from MaxFileSize property
  1105.     If lMaxFileSize <> 255 Then  'default is 255
  1106.         tOpenFile.nMaxFile = 255
  1107.     End If
  1108.             
  1109.     'lpstrFileTitle As String - init from FileTitle property
  1110.         'prepare sFileTitleBuff
  1111.         sFileTitleBuff = sFileTitle
  1112.         'pad with spaces
  1113.         While Len(sFileTitleBuff) < lMaxSize - 1
  1114.             sFileTitleBuff = sFileTitleBuff & " "
  1115.         Wend
  1116.         'trim to length of lMaxFileSize - 1
  1117.         If lMaxFileSize = 0 Then
  1118.             sFileTitleBuff = Mid$(sFileTitleBuff, 1, lMaxSize - 1)
  1119.         Else
  1120.             sFileTitleBuff = Mid$(sFileTitleBuff, 1, lMaxFileSize - 1)
  1121.         End If
  1122.         'null terminate
  1123.         sFileTitleBuff = sFileTitleBuff & Chr$(0)
  1124.     tOpenFile.lpstrFileTitle = sFileTitleBuff
  1125.         
  1126.     'tOpenFile.lpstrInitialDir As String - init from InitDir property
  1127.     tOpenFile.lpstrInitialDir = sInitDir
  1128.     
  1129.     'tOpenFile.lpstrTitle As String - init from DialogTitle property
  1130.     tOpenFile.lpstrTitle = sDialogTitle
  1131.     
  1132.     'tOpenFile.flags As Long - init from Flags property
  1133.     tOpenFile.flags = lFlags
  1134.         
  1135.     'tOpenFile.lpstrDefExt As String - init from DefaultExt property
  1136.     tOpenFile.lpstrDefExt = sDefaultExt
  1137.     
  1138.     
  1139.     '***    call the GetOpenFileName API function
  1140.     Select Case iAction
  1141.         Case 1  'ShowOpen
  1142.             lApiReturn = GetOpenFileName(tOpenFile)
  1143.         Case 2  'ShowSave
  1144.             lApiReturn = GetSaveFileName(tOpenFile)
  1145.         Case Else   'unknown action
  1146.             Exit Sub
  1147.     End Select
  1148.     m_cancelled = False
  1149.     
  1150.     '***    handle return from GetOpenFileName API function
  1151.     Select Case lApiReturn
  1152.         
  1153.         Case 0  'user canceled
  1154.  
  1155.             'generate an error
  1156.             m_cancelled = True
  1157.             Exit Sub
  1158.         
  1159.         Case 1  'user selected or entered a file
  1160.             'sFileName gets part of tOpenFile.lpstrFile to the left of first Chr$(0)
  1161.             sFileName = sLeftOfNull(tOpenFile.lpstrFile)
  1162.             sFileTitle = sLeftOfNull(tOpenFile.lpstrFileTitle)
  1163.         
  1164.         Case Else   'an error occured
  1165.             'call CommDlgExtendedError
  1166.             lExtendedError = CommDlgExtendedError
  1167.         
  1168.     End Select
  1169.     
  1170.  
  1171. Exit Sub
  1172.  
  1173. ShowFileDialogError:
  1174.     
  1175.     Exit Sub
  1176.  
  1177. End Sub
  1178.  
  1179. Public Property Get Cancelled() As Boolean
  1180.     
  1181.     Cancelled = m_cancelled
  1182.     
  1183. End Property
  1184.  
  1185. Public Property Let Cancelled(vNewValue As Boolean)
  1186.     m_cancelled = vNewValue
  1187.     
  1188. End Property
  1189.